using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Jaws.Core.Web.Entities;
using Jaws.Core.Web.Entities.Pages;
using Jaws.Core.Web.Entities.Post;
using Jaws.Core.Web.Data;

namespace Jaws.Web.Mvc.Controllers
{
    public class PageController : Controller
    {
        //
        // GET: /Page/

        public ActionResult Index()
        {
            Page page = XmlCollectionBase<PageCollection, Page>.Load().FirstOrDefault(p => p.IsHomePage);
            return page != null ? View("Details", page) : View("error404");
        }

        //
        // GET: /Page/Details/5
        public ActionResult Details(string id)
        {
            Page page = id.IsGuid() ? XmlCollectionBase<PageCollection, Page>.Load().First(p => p.Id.ToString() == id) : XmlCollectionBase<PageCollection, Page>.Load().FirstOrDefault(p => p.Slug == id);

            if (page == null)
            {
                return RedirectToAction("Index", "Page");
            }
            return View("Details", page);
        }

        //
        // GET: /Page/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Page/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Page/Edit/5

        public ActionResult Edit(Guid id)
        {
            return View();
        }

        //
        // POST: /Page/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}